<?php
require 'session.php';
include 'loginrequired.php';

//db connect
require 'connect_pdo.php';
require 'classes.php';
$user_id = $_SESSION['user'];

$pdo = new pdo_query;

//fill array with preference short names/numbers
$prefs_ar = array(1=>'Hide_inactive', 2=>'Show_primary', 3=>'Show_secondary', 4=>'Warning_date', 5=>'Email_warning');
$prefsCount = count($prefs_ar);

$sqlSelectPrefs = "SELECT pref_number, preference, value FROM user_preferences WHERE user_id = ? and pref_number = ?";
$sqlInsertPrefs = "INSERT INTO user_preferences (pref_number, preference, user_id, datestamp, userstamp) VALUES (?,?,?,(SELECT NOW()),1)";
$sqlUpdatePrefs = "UPDATE user_preferences SET value = ? WHERE user_id = ? AND preference = ?";

//insert the preference record with all defaults if it's missing
foreach ($prefs_ar as $key=>$value)
{
	
	$pdo->bound_query($sqlSelectPrefs,array($user_id,$key),'All');
	$prefs = $pdo->result;

	if (empty($prefs))
	{
		$pdo->bound_query($sqlInsertPrefs,array($key,$value,$user_id),'None');
		
	}

}
//then display

echo "<table class='acclist'>";
echo "<tr><th>Preference </th><th></th></tr>";

foreach ($prefs_ar as $key=>$value)
{
	
		$pdo->bound_query($sqlSelectPrefs,array($user_id, $key),'All');
		$prefsResult = $pdo->result;
		$prefName[]=$prefsResult[0][1];//make an array of the names
		$prefVal[]=$prefsResult[0][2];//make an array of the values
		
	
}

//full names of preferences, array position and is_checkbox
$prefComb = array_combine($prefName,$prefVal);
$prefs_ar_full = 
array(
1=>array($prefs_ar[1],'Hide inactive characters',$prefVal[0],1), 
2=>array($prefs_ar[2],'Show primary professions',$prefVal[1],1), 
3=>array($prefs_ar[3],'Show secondary professions',$prefVal[2],1), 
4=>array($prefs_ar[4],'Days before showing account warning',$prefVal[3],0),
5=>array($prefs_ar[5],'Email after warning days exceeded',$prefVal[4],1)
);

//build form from arrays
echo "<form method='post'>";
$i=1;
while ($i<=$prefsCount)//number of preferences 
{
$currentVal = $prefs_ar_full[$i][2];
$checkbox = $prefs_ar_full[$i][3];
$shortName = $prefs_ar_full[$i][0];
$longName = $prefs_ar_full[$i][1];

if ($currentVal==1)
{$checked='checked';}
else
{$checked='';}

echo "<tr><td>".$longName."</td><td>";

if ($checkbox==1)
{echo "<input type='checkbox' name='".$shortName."' value='1' $checked/></td></tr>";}
else
{echo "<input type='text' name='".$shortName."' value='".$currentVal."'/></td></tr>";}

$i++;
}

echo "<tr><td colspan='2'><input type='submit' name='prefs' value='save'/></td>";
echo "</form>";
echo "</table>";

//display preferences
if (isset($_POST['prefs']))
{	
$i=1;
while ($i<=$prefsCount)//number of preferences 
	{
$currentVal = $prefs_ar_full[$i][2];
$checkbox = $prefs_ar_full[$i][3];
$shortName = $prefs_ar_full[$i][0];
$longName = $prefs_ar_full[$i][1];
	
$prefPostValue = $_POST[$prefs_ar_full[$i][0]];	
if ($prefPostValue<>1 && $checkbox==1)
$prefInsertvalue=0;
else if ($checkbox==0)
{$prefInsertvalue=$prefPostValue;}
else
$prefInsertvalue=1;

$pdo->bound_query($sqlUpdatePrefs,array($prefInsertvalue,$user_id,$shortName),'None');
$i++;
}
	echo "<p>Preferences saved</p>";

}

?>